/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.wb.internal.dev;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle.
 */
public class Activator extends AbstractUIPlugin {
  public static final String PLUGIN_ID = "org.eclipse.wb.dev";
  private static Activator plugin;

  ////////////////////////////////////////////////////////////////////////////
  //
  // Life cycle
  //
  ////////////////////////////////////////////////////////////////////////////
  @Override
  public void start(BundleContext context) throws Exception {
    super.start(context);
    plugin = this;
  }

  @Override
  public void stop(BundleContext context) throws Exception {
    plugin = null;
    super.stop(context);
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Access
  //
  ////////////////////////////////////////////////////////////////////////////
  public static Activator getDefault() {
    return plugin;
  }

  public static ImageDescriptor getImageDescriptor(String path) {
    return imageDescriptorFromPlugin(PLUGIN_ID, path);
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Logging
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Logs given {@link IStatus} into Eclipse .log.
   */
  public static void log(IStatus status) {
    getDefault().getLog().log(status);
  }

  /**
   * Logs {@link IStatus} with given message into Eclipse .log.
   */
  public static void log(String message) {
    log(new Status(IStatus.INFO, PLUGIN_ID, IStatus.INFO, message, null));
  }

  /**
   * Logs {@link IStatus} with given exception into Eclipse .log.
   */
  public static void log(Throwable e) {
    String message = e.getMessage();
    if (message == null) {
      message = e.getClass().getName();
    }
    log("Designer Internals: " + message, e);
  }

  /**
   * Logs {@link IStatus} with given message and exception into Eclipse .log.
   */
  public static void log(String message, Throwable e) {
    log(createStatus(message, e));
  }

  /**
   * Creates {@link IStatus} for given message and exception.
   */
  private static Status createStatus(String message, Throwable e) {
    return new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, message, e) {
      @Override
      public boolean isMultiStatus() {
        return true;
      }
    };
  }
}
